﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Etapa1.SteeringExperiments
{

    public class PathDescription
    {
        public PathDescription()
        {
        }
        bool closed;

        public bool Closed
        {
            get { return closed; }
            set { closed = value; }
        }
        List<Vector3> points;

        public List<Vector3> Points
        {
            get { return points; }
            set { points = value; }
        }
    }

    public class FollowPath : Steering
    {

        public FollowPath(PathDescription pd, float distanceBeforeNextWaypoint)
            : base(BehaviorType.follow_path)
        {
            this.distanceBeforeNextWaypoint = distanceBeforeNextWaypoint;
            this.pd = pd;
            seek = new Seek(pd.Points[0]);
            currentIndex = 0;
        }

        float distanceBeforeNextWaypoint;
        PathDescription pd;
        int currentIndex;
        Seek seek;

        public override Microsoft.Xna.Framework.Vector3 CalculateSteering(SteeringEntityInfo entity)
        {
            if(currentIndex != pd.Points.Count - 1)
            {
                if (Vector3.Distance(pd.Points[currentIndex], entity.Obj.Position) < distanceBeforeNextWaypoint)
                {
                    currentIndex++;
                    seek.Target = pd.Points[currentIndex];
                }            

            }
            else if (currentIndex == pd.Points.Count - 1)
            {
                if (pd.Closed == true)
                {
                    if (Vector3.Distance(pd.Points[currentIndex], entity.Obj.Position) < distanceBeforeNextWaypoint)
                    {
                        currentIndex = 0;
                        seek.Target = pd.Points[currentIndex];
                    }
                }
                else
                {
                    this.SteeringState = SteeringState.Innactive;
                }
            }

            return seek.CalculateSteering(entity);
        }

    }
}
